using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FlickerColor : KnowsWhenSeen
{
	public FlickerColorStep[] m_FlickerColorSteps;
	public bool m_EffectsJustOneMatSlot;
	public int m_EffectsWhichMatSlot;

	private int m_currStepIdx = -1;
	private int m_matId;
	private float m_currCountdown = 0.01f;

	void Start()
	{
		m_matId = Shader.PropertyToID( "_Color" );
	}

	void Update()
	{
		if ( m_currCountdown > 0 )
		{
			m_currCountdown -= Time.deltaTime;
			if ( m_currCountdown <= 0f )
			{
				if ( m_FlickerColorSteps.Length > 0 )
				{
					m_currStepIdx++;
					if ( m_currStepIdx > m_FlickerColorSteps.Length - 1 )
					{
						m_currStepIdx = 0;
					}

					SetColor( m_FlickerColorSteps[ m_currStepIdx ].m_Color );
					m_currCountdown = m_FlickerColorSteps[ m_currStepIdx ].m_Seconds;
				}
			}
		}
	}

	void SetColor( Color col )
	{
		if ( renderer != null )
		{
			if ( renderer.materials != null )
			{
				if ( m_EffectsJustOneMatSlot )
				{
					if ( m_EffectsWhichMatSlot < renderer.materials.Length ) // not past end
					{
						renderer.materials[m_currStepIdx].SetColor( m_matId, col );
					}
					else Debug.LogWarning( "DRAT! FlickerColor behavior on '" + name + "' is doing nothing since the slot # " + m_EffectsWhichMatSlot + " is bigger than the list of materials the Mesh renderer has. " );
				}
				else
				{
					foreach ( Material mat in renderer.materials )
					{
						mat.SetColor( m_matId, col );
					}
				}
			}
		}
	}

}

[System.Serializable]
public class FlickerColorStep
{
	public Color m_Color;
	public float m_Seconds;
}